package cn.vcjf.washcar.washcarorder.controller;


import cn.vcjf.washcar.common.util.IdGen;
import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.server.service.ServerInfoService;
import cn.vcjf.washcar.system.entity.SysUser;
import cn.vcjf.washcar.washcarorder.entity.WashCarOrder;
import cn.vcjf.washcar.washcarorder.service.WashCarOrderService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 洗车订单表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-17
 */
@Controller
@RequestMapping("/washcarorder/washCarOrder")
public class WashCarOrderController {
	@Resource
	private WashCarOrderService washCarOrderService;
	@Resource
	private ServerInfoService serverInfoService;

	@RequestMapping("/addWashCarOrder")
	@ResponseBody
	public TableResult addWashCarOrder(WashCarOrder washCarOrder) {
		washCarOrder.preInsert();
		SysUser user = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
		washCarOrder.setUserId(user.getId());
		washCarOrder.setOrderNo(IdGen.getAutoOrderNo());
		boolean b = washCarOrderService.insert(washCarOrder);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateWashCarOrder")
	@ResponseBody
	public TableResult updateWashCarOrder(WashCarOrder washCarOrder) {
		washCarOrder.preUpdate();
		boolean b = washCarOrderService.updateById(washCarOrder);
		return TableResult.Result(b);
	}

	@RequestMapping("/findWashCarOrderOne")
	@ResponseBody
	public TableResult findWashCarOrderOne(WashCarOrder washCarOrder) {

		EntityWrapper<WashCarOrder> wrapper = washCarOrder.getQueryWrapper();

		WashCarOrder result = washCarOrderService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findWashCarOrderList")
	@ResponseBody
	public TableResult findWashCarOrderList(WashCarOrder washCarOrder) {
		Page<WashCarOrder> page = washCarOrder.getPageInfo();

		List<WashCarOrder> washCarOrderList = washCarOrderService.selectPageList(page,washCarOrder);
		page.setRecords(washCarOrderList);
		return TableResult.Result(page);

	}

	@RequestMapping("/orderDetail")
	public ModelAndView orderDetail(WashCarOrder washCarOrder, ModelAndView model) {

		Page<WashCarOrder> page = washCarOrder.getPageInfo();

		List<WashCarOrder> washCarOrderList = washCarOrderService.selectPageList(page,washCarOrder);
		if(washCarOrderList!=null&&!washCarOrderList.isEmpty()){
			washCarOrder = washCarOrderList.get(0);
		}
		model.addObject("washCarOrder",washCarOrder);
		model.setViewName("orderDetail");
		return model;
	}

	@RequestMapping("/deleteWashCarOrder")
	@ResponseBody
	public TableResult deleteWashCarOrder(WashCarOrder washCarOrder) {
		washCarOrder.preUpdate();
		boolean b = washCarOrderService.deleteById(washCarOrder.getId());
		return TableResult.Result(b);
	}
	@RequestMapping("/deleteWashCarOrderByIds")
	@ResponseBody
	public TableResult deleteWashCarOrderByIds(String... ids) {
		List<String> list = Arrays.asList(ids);
		boolean b = washCarOrderService.deleteBatchIds(list);
		return TableResult.Result(b);
	}

	@RequestMapping("/findWashCarOrderStatistics")
	@ResponseBody
	public TableResult findWashCarOrderStatistics() {
		Map<String, Double> b = washCarOrderService.findWashCarOrderStatistics();
		return TableResult.Result(b);
	}

	/**
	 * 导出订单数据表格
	 * @param washCarOrder
	 * @param response
	 */
	@RequestMapping("/exportWashCarOrder")
	public void exportWashCarOrder(WashCarOrder washCarOrder, HttpServletResponse response) {

		washCarOrderService.exportWashCarOrder(washCarOrder, response);
	}

}
